<?php

namespace Home\Controller;

use Think\Controller;

class IdeaController extends Controller {
    // function __destruct(){                                                                         // 析构方法  写入用户访问和操作日志
    //     require '/home/wwwroot/default/world/behavior.php';
    // }
	// 全部创意列表
	public function showList() {
		$showmodel = M ( 'ideas' ); // 实例化ideas
		$users = M ( 'users' ); // 实例化users
		
		$number = $showmodel->where ( 'ispublic=0' )->count (); // 有多少公开的创意
		$show = $showmodel->where ( 'ispublic=0' )->order ( 'idate desc' )->select (); // 查询公开的创意按发布时间排序
		for($i = 0; $i < $number; $i ++) { // 有多少公开的创意就循环多少次
			$idate = $show [$i] ['idate']; // 取出发布时间
			$sub = substr ( $idate, 11, 2 ); // 将发布时间的时截取出来
			if ($sub < 12) {
				$show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
				$show [$i] ['num'] = $i; // 计数
				$uid = $show [$i] ['uid'];
				$iid = $show [$i] ['iid'];
				$show [$i] ['realname'] = $users->where ( "uid = $uid" )->getField ( 'realname' ); // 查询该创意用户的真实姓名
				$content = $showmodel->where ( "iid = $iid" )->getField ( 'content' ); // 取出内容
				$str = strip_tags ( $content ); // 过滤html标签
				$str = str_replace ( " ", "", "$str" ); // 过滤空格
				$result = mb_substr ( $str, 0, 50, 'utf-8' ); // 截取内容
				$show [$i] ['synopsis'] = $result;
			} else {
				$show [$i] ['time'] = "下午";
				$show [$i] ['num'] = $i;
				$uid = $show [$i] ['uid'];
				$iid = $show [$i] ['iid'];
				$show [$i] ['realname'] = $users->where ( "uid = $uid" )->getField ( 'realname' );
				$content = $showmodel->where ( "iid = $iid" )->getField ( 'content' );
				$str = strip_tags ( $content );
				$str = str_replace ( " ", "", "$str" );
				$result = mb_substr ( $str, 0, 50, 'utf-8' );
				$show [$i] ['synopsis'] = $result;
			}
		}
		$this->assign ( 'show', $show );
		$this->display ();
	}
	
	// 创意详细
	public function details() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Comment = new \Home\Controller\CommentController (); // 实例化Comment
		
		$details = M ( 'ideas' ); // 实例化ideas
		$user = M ( 'users' ); // 实例化users
		$iid = $_REQUEST ['iid']; // 取创意的id
		$uid = session ( 'userUid' ); // session取uid
		$role = $user->where ( "uid=$uid" )->getField ( 'role' ); // 取身份标识位
		
		if ($role != 3) {
			$result = $details->where ( "iid=$iid" )->getField ( 'uid' ); // 取uid
			if ($result == $uid) {
				$idea = $details->where ( "iid=$iid" )->select (); // 查询符合条件的创意详细
				$users = $user->where ( "uid=$result" )->select (); // 查询用户信息
			} else {
				$idea = $details->where ( "iid=$iid and ispublic=0" )->select (); // 查询符合条件的创意详细
				if (null == $idea) {
					$Hint->myerror ( "此创意不公开！", "2" );
					die ();
				} else {
					$users = $user->where ( "uid=$result" )->select (); // 查询用户信息
				}
				$users = $user->where ( "uid=$result" )->select (); // 查询用户信息
			}
			$title = $idea [0] ['idea']; // 标题
			$Comment->showlist (); // 调用评论列表
			$details->where ( "iid=$iid" )->setInc ( 'hint', 1 ); // 页面刷新一次数据库hint字段就加1
		} else { // 管理员端入口
			$result = $details->where ( "iid=$iid" )->getField ( 'uid' ); // 取uid
			$idea = $details->where ( "iid=$iid" )->select (); // 查询符合条件的创意详细
			$users = $user->where ( "uid=$result" )->select (); // 查询用户信息
			$title = $idea [0] ['idea']; // 标题
		}
		
		$this->assign ( 'users', $users );
		$this->assign ( 'title', $title );
		$this->assign ( 'idea', $idea );
		$this->display ();
	}
	
	// 发布创意
	public function addIdeas() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		$users=M('users');
		$number=1;
		$adviser=$users->where('is_teacher= 1')->order('uid desc')->select();
		$count=$users->where('is_teacher= 1')->order('uid desc')->select();
	    $this->assign('adviser',$adviser);
	    $this->assign('number',$number);
		if (! empty ( $_POST )) { // 判断表单是否有数据
			$users=M('users');
			$addideas = M ( 'ideas' ); // 实例化ideas
			
			if (null != $_FILES ['attach'] ['name']) { // 判断文件是否被修改，如果为空则不调用upload方法
				$info = $Hint->upload ( 'attach' ); // 调用upload方法
				$attachname = $info ['attach'] ['savepath'] . $info ['attach'] ['savename']; // 存储附件的子目录和名字
			}
			
			if ($_POST ['optionsRadios1'] == '1') { // 判断是否公开
				$ispublic = 0;
			} else {
				$ispublic = 1;
			}
			if ($_POST ["optionsRadios2"] == '1') { // 判断是否允许评论
				$iscomment = 0;
			} else {
				$iscomment = 1;
			}
			
			if (strlen ( $_POST ['idea'] ) > 60) { // 判断标题是否过长
				$Hint->myerror ( "标题太长！", "2" );
				die ();
			}
			//$adviser=$users->where("is_teacher")->order()->select();
			$uid = session ( 'userUid' ); // session取uid
			$username = session ( 'userUsername' ); // session取用户名
			
			if ($addideas->create ()) { // 创建数据对象
				$addideas->uid = $uid;
				$addideas->ideaman = $username;
				$addideas->ispublic = $ispublic;
				$addideas->iscomment = $iscomment;
				$addideas->attach = $attachname; // 附件名
				$addideas->idate = date ( "Y-m-d H:i:s" ); // 生成发布日期
				if (false !== $addideas->add ()) { // 添加数据库
					$Hint->mysuccee ( "发布成功！", "1", "Home/Idea/myIdea" ); // 成功跳转到我的创意
					die ();
				} else {
					$Hint->myerror ( "发布失败！", "2" );
					die ();
				}
			} else {
				$Hint->myerror ( "创建数据对象失败！", "2" );
				die ();
			}
		} else {
			$this->display ();
		}
	}
	
	// 我的创意列表
	public function myIdea() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		
		$number = 1; // 计数器，前台编号累加值
		$myIdea = M ( 'ideas' ); // 实例化ideas
		$userUid = session ( 'userUid' ); // session取uid
		
		$myidea = $myIdea->where ( "uid=$userUid" )->order ( 'idate desc' )->select (); // 查询相应学生的创意
		$this->assign ( 'myidea', $myidea );
		$this->assign ( 'number', $number );
		$this->display ();
	}
	
	// 我评论的创意
	public function myComment() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		
		$number = 1; // 计数器，前台编号累加值
		$myComment = M ( 'ideas' ); // 实例化ideas
		$comment = M ( 'comments' ); // 实例化comments
		$uid = session ( 'userUid' ); // session取uid
		
		$iid = $comment->distinct ( true )->where ( "uid=$uid" )->field ( 'iid' )->select (); // 取出iid的值，剔除重复的iid
		for($i = 0; $i < sizeof ( $iid ); $i ++) {
			$ciid = $iid [$i] ['iid'];
			$mycomment [$i] ['iid'] = $myComment->where ( "iid = $ciid" )->getField ( 'iid' ); // 查询符合条件的值
			$mycomment [$i] ['idea'] = $myComment->where ( "iid = $ciid" )->getField ( 'idea' ); // 查询符合条件的值
			$mycomment [$i] ['idate'] = $myComment->where ( "iid = $ciid" )->getField ( 'idate' ); // 查询符合条件的值
			$mycomment [$i] ['ideaman'] = $myComment->where ( "iid = $ciid" )->getField ( 'ideaman' ); // 查询符合条件的值
		}
		
		$this->assign ( 'mycomment', $mycomment );
		$this->assign ( 'number', $number );
		$this->display ();
	}
	
	// 修改创意
	public function alterIdea() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		
		$alterIdea = M ( 'ideas' ); // 实例化ideas
		$iid = $_REQUEST ['iid']; // 从前台页面取出iid
		$uid = session ( 'userUid' ); // session取uid
		
		if ($uid == null) {
			die ( "非法操作!" );
		}
		
		if (! empty ( $_POST )) { // 判断表单是否为空
			if (null != $_FILES ['attach'] ['name']) { // 判断文件是否被修改，如果为空则不调用upload方法
				$filename = $alterIdea->where ( "iid=$iid" )->getField ( 'attach' ); // 从数据库中取出附件名
				$file = './Public/UPLOADS/attach/' . "$filename"; // 把目录名存到file中
				unlink ( $file ); // 删除真实数据
				
				$info = $Hint->upload ( 'attach' ); // 调用upload方法
				$attachname = $info ['attach'] ['savepath'] . $info ['attach'] ['savename']; // 存储附件的子目录和名
				$date ['attach'] = $attachname; // 把附件的名存储到attach字段
			}
			
			$date ['idea'] = $_POST ['idea'];
			$date ['content'] = $_POST ['content'];
			if ($_POST ['optionsRadios1'] == '1') {
				$date ['ispublic'] = 0;
			} else {
				$date ['ispublic'] = 1;
			}
			if ($_POST ["optionsRadios2"] == '1') {
				$date ['iscomment'] = 0;
			} else {
				$date ['iscomment'] = 1;
			}
			
			$result = $alterIdea->where ( "iid=$iid and uid=$uid" )->save ( $date ); // 更新数据
			if ($result) {
				$Hint->mysuccee ( "修改成功！", "1", "Home/Idea/myIdea" ); // 修改成功跳转到我的创意
				die ();
			} else {
				$Hint->myerror ( "没有信息被修改！", "2" );
				die ();
			}
		} else {
			$myidea = $alterIdea->where ( "iid=$iid and uid=$uid" )->select ();
			$this->assign ( 'myidea', $myidea );
			$this->display ();
		}
	}
	
	// ajax 动态修改创意允许评论和公开
	public function ajaxAlterIdea() {
		
		// type : C = 评论 P = 公开
		// iid : 创意编号
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		
		$resultsInfo = null;
		
		$alterIdea = M ( 'ideas' ); // 实例化ideas
		$uid = session ( 'userUid' ); // session取uid
		
		$type = I ( 'post.type' );
		$iid = I ( 'post.iid' );
		// echo 'uid = ' .$uid;
		// echo ' iid = '.$iid;
		// echo ' type = '.$type;
		
		if ($uid == null || $iid == null || $type == null) {
			die ( "非法访问!" );
		}
		
		// 判断评论是否公开
		if ($type == "C") {
			
			$resultsInfo = null;
			
			// 取出现有状态
			$ideaDate = $alterIdea->where ( "iid=$iid and uid=$uid" )->find ();
			$iscomment = $ideaDate ['iscomment'];
			
			// 判断现有状态并更新
			if ($iscomment == 0) {
				$iscomment = 1;
				$resultsInfo = "C1";
			} else if ($iscomment == 1) {
				$iscomment = 0;
				$resultsInfo = "C0";
			}
			$ideaDate ['iscomment'] = $iscomment;
			$result = $alterIdea->where ( "iid=$iid and uid=$uid" )->save ( $ideaDate ); // 更新数据
			                                                                             
			// 判断更新是否成功
			if ($result) {
				echo $resultsInfo;
				die ();
			} else {
				die ( "更新失败" );
			}
		} else if ($type == "P") {
			
			// 取出现有状态
			$ideaDate = $alterIdea->where ( "iid=$iid and uid=$uid" )->find ();
			$ispublic = $ideaDate ['ispublic'];
			
			// 判断现有状态并更新
			if ($ispublic == 0) {
				$ispublic = 1;
				$resultsInfo = "P1";
			} else if ($ispublic == 1) {
				$ispublic = 0;
				$resultsInfo = "P0";
			}
			$ideaDate ['ispublic'] = $ispublic;
			$result = $alterIdea->where ( "iid=$iid and uid=$uid" )->save ( $ideaDate ); // 更新数据
			                                                                             
			// 判断更新是否成功
			if ($result) {
				echo $resultsInfo;
				die ();
			} else {
				die ( "更新失败" );
			}
		}
	}
	
	// 删除创意
	public function ideaDel() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		
		$ideaDel = M ( 'ideas' ); // 实例化ideas
		$comment = M ( 'comments' ); // 实例化comments
		$iid = $_REQUEST ['iid']; // 取iid
		$uid = session ( 'userUid' ); // session取uid
		
		$filename = $ideaDel->where ( "iid=$iid" )->getField ( 'attach' ); // 从数据库中取出附件名
		$file = './Public/UPLOADS/attach/' . "$filename"; // 把目录名存到file中
		unlink ( $file ); // 删除真实数据
		
		$results = $ideaDel->where ( "iid=$iid and uid=$uid" )->delete (); // 根据条件删除创意
		if ($results) {
			$comment->where ( "iid=$iid" )->delete (); // 根据条件删除评论
			echo "0";
			die ();
		} else {
			echo "1";
			die ();
		}
	}
}